<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header class="home-header">
      <div>
        <img src="../assets/logo.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边区域 -->
      <el-aside :width="isCollapse ? '60px' : '200px'" class="home-aside">
        <!-- collapse折叠区域 -->
        <div class="collapse-btn" @click="toggleCollapse">|||</div>

        <el-menu background-color="#263238" text-color="#fff" active-text-color="#007ACC" :collapse="isCollapse" :collapse-transition='false' router :default-active='activePath'>
          <!-- 一级导航 -->
          <el-submenu :index="item.id +'' " :key="item.id" v-for="item in menulist">
            <!-- 一级导航模板区域 -->
            <template slot="title">
              <!-- 一级导航图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 一级文本 -->
              <span class="aside-span">{{item.authName}}</span>
            </template>
            <!-- 二级导航 -->
            <el-menu-item-group>
              <el-menu-item
                :index="'/'+subItem.path"
                :key="subItem.id"
                v-for="subItem in item.children"
               @click="saveNavState('/'+subItem.path)">
                <template slot="title">
                  <!-- 二级导航图标 -->
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main class="home-main">
        <!-- 路由站位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse:false,
      activePath:''
    }
  },
  //创建生命周期函数
  created() {
    this.getMenulist(),
    this.activePath = window.sessionStorage.getItem('activePath')
    
  },
  methods: {
    logout() {
      //点击退出,清空sessionStorage
      window.sessionStorage.clear()
      this.$router.push('/login') //编程式导航跳转的登录页
    },
    //  getMenulist(){
    // const data =  this.$http.get('menus')//.get返回的是一个promise,可以用async await 简化操作
    // console.log(data);//是promise
    // }
    async getMenulist() {
      const { data: res } = await this.$http.get('menus') //.get返回的是一个promise,可以用async await 简化操作
      //  console.log(res);//把res.data挂载到 data上
      if (res.meta.status != 200) return this.$message.error(res.meta.status)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse//this不要忘记
      // console.log(this.isCollapse)
      },
      //保存链接的激活状态
  saveNavState(activePath){
    window.sessionStorage.setItem('activePath',activePath),
    this.activePath = activePath
  }

  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.home-header {
  background-color: #263238;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  img {
    height: 60px;
    width: 60px;
    border: 1px solid #eee;
    border-radius: 50%;
  }
  span {
    align-items: center;
    color: #fff;
    font-size: 20px;
    margin-left: 10px;
  }
  div {
    display: flex;
    align-items: center;
  }
}
.home-aside {
  background-color: #263238;
}
.aside-span {
  margin-left: 10px;
}
.iconfont {
  font-size: 16px;
}
.collapse-btn {
  background-color: #4A5064;
  text-align: center;
  color:#eee;
  height: 24px;
  line-height: 22px;
  font-size: 15px;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
</style>